package cn.itcast.web.action;

import cn.itcast.crm.domain.Customer;
import cn.itcast.domain.base.Area;
import cn.itcast.domain.constant.Constants;
import cn.itcast.domain.delivery.Order;
import cn.itcast.web.action.common.BaseAction;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;

@Namespace("/")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {

    private String sendAreaInfo;//发件人省市区
    private String recAreaInfo;//收件人省市区

    public void setSendAreaInfo(String sendAreaInfo) {
        this.sendAreaInfo = sendAreaInfo;
    }

    public void setRecAreaInfo(String recAreaInfo) {
        this.recAreaInfo = recAreaInfo;
    }

    //"order_add.action
    @Action(value = "order_add", results = {@Result(name = "success", location = "index.html", type = "redirect")})
    public String add() {
        //处理省市区 省/市/区/ 封装进area对象
        Area sendArea = new Area();
        String[] sendData = sendAreaInfo.split("/");
        sendArea.setProvince(sendData[0]);
        sendArea.setCity(sendData[1]);
        sendArea.setDistrict(sendData[2]);
        Area recArea = new Area();
        String[] recData = recAreaInfo.split("/");
        recArea.setProvince(recData[0]);
        recArea.setCity(recData[1]);
        recArea.setDistrict(recData[2]);
        //将处理好的数据封装进model对象
        model.setSendArea(sendArea);
        model.setRecArea(recArea);
        //关联当前登录的客户 从session中获取
        Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
        //关联数据
        model.setCustomer_id(customer.getId());
        //同伙webService 将model 对象保存到
        WebClient.create(Constants.BOS_HOST+Constants.BOS_URL+"/services/orderService/order/save")
                .type(MediaType.APPLICATION_JSON).post(model);

        return SUCCESS;
    }
}
